#######################################################
#####REPLICATION ARCHIVE FOR NUANCED ACCOUNTABILITY####
#########BRITISH JOURNAL OF POLITICAL SCIENCE##########
#################WARD DATA, FIGURES####################
###de Kadt & Lieberman, 2017

#######################################################
################ SETUP BEGINS HERE ####################
#######################################################
###Packages
library(foreign)

###Working Directories###
## USER: Input your own working directory for files on the next line ##
dir = paste("####INPUT WORKING FILE DIRECTORY BETWEEN QUOTATION MARKS ####")

##

fig = paste(dir,"output/", sep="")
stgz =  paste(dir,"output/", sep="")

###Data Setup###
#Load dataset
dat = read.dta(paste(dir,"nuanced_accountability_ward.dta", sep=""))

###Paper Figure 1:
pdf(file=paste(fig,"levels.pdf",sep=""), width=9, height=8)
  par(mfrow=c(2,2), mar=c(4,4,4,2)+0.1)
    plot(density(na.omit(dat$home_water_perc2001)), lwd=2, col="blue", xlab="Fraction households covered", main="Water", xlim=c(0,1))
      lines(density(na.omit(dat$home_water_perc2011)), lwd=2, col="blue", lty=3)
      abline(v= mean(na.omit(dat$home_water_perc2001)), lwd=2, col="blue",)
      abline(v= mean(na.omit(dat$home_water_perc2011)), lwd=2, col="blue", lty=3)
      legend(.5,3, c("2001", "2011"), lwd=2, lty=c(1,3), bty="n", col="blue")

    plot(density(na.omit(dat$all_flush_perc2001)), lwd=2, col="dark green", xlab="Fraction households covered", main="Toilets", xlim=c(0,1))
      lines(density(na.omit(dat$all_flush_perc2011)), lwd=2, col="dark green", lty=3)
      abline(v= mean(na.omit(dat$all_flush_perc2001)), lwd=2, col="dark green",)
      abline(v= mean(na.omit(dat$all_flush_perc2011)), lwd=2, col="dark green", lty=3)
      legend(.5,2, c("2001", "2011"), lwd=2, lty=c(1,3), bty="n", col="dark green")

  plot(density(na.omit(dat$refuse_perc2001)), lwd=2, col="dark red", xlab="Fraction households covered", main="Refuse", xlim=c(0,1))
    lines(density(na.omit(dat$refuse_perc2011)), lwd=2, col="dark red", lty=3)
    abline(v= mean(na.omit(dat$refuse_perc2001)), lwd=2, col="dark red",)
    abline(v= mean(na.omit(dat$refuse_perc2011)), lwd=2, col="dark red", lty=3)
    legend(.5,2.2, c("2001", "2011"), lwd=2, lty=c(1,3), bty="n", col="dark red")

  plot(c(1:10.5),c(1:10.5), axes=F, xlab="", ylab="", type="n")
    text(1, 10, "Water:", pos=4, cex=1.3, col="blue")
     text(1, 9.4, paste("Mean change =",round(mean(na.omit(dat$home_water_perc2011))-mean(na.omit(dat$home_water_perc2001)),3)*100, "%age points", sep=" "), pos=4, cex=1.3, col="blue")
    text(1, 8.8, "Est. increase in hhs covered = 1.5 mill", pos=4, cex=1.3, col="blue")
    text(1, 7, "Toilet:", pos=4, cex=1.3, col="dark green")
     text(1, 6.4, paste("Mean change =",round(mean(na.omit(dat$all_flush_perc2011))-mean(na.omit(dat$all_flush_perc2001)),3)*100, "%age points", sep=" "), pos=4, cex=1.3, col="dark green")
    text(1, 5.8, "Est. increase in hhs covered = 936,000", pos=4, cex=1.3, col="dark green")
    text(1, 4, "Refuse:", pos=4, cex=1.3, col="dark red" )
     text(1, 3.4, paste("Mean change =",round(mean(na.omit(dat$refuse_perc2011))-mean(na.omit(dat$refuse_perc2001)),3)*100, "%age points", sep=" "), pos=4, cex=1.3, col="dark red")
    text(1, 2.8, "Est. increase in hhs covered = 611,000", pos=4, cex=1.3, col="dark red")
dev.off()


###Appendix Figure 1:
pdf(file=paste(fig,"anc_vote_change.pdf",sep=""), width=7, height=6)
  par(mfrow=c(1,1), mar=c(4,4,4,2)+0.1)
    plot(density(na.omit(dat$lgeanc_vs_ch10)), lwd=3, col="blue",  main="", ylim=c(0,5), axes=F, xlab="Change in ANC vote share")
      lines(density(na.omit(dat$npeanc_vs_ch10)), lwd=3,  col="red")
      axis(1)
      axis(2)
      abline(v=0, lwd=2, lty=3, col="gray")
      legend(0.05,4.8, c("Change in Local ANC_vs", "Change in National ANC_vs"), lwd=3, bty="n", col=c("blue", "red"))
dev.off()





